﻿using AutoMapper;
using MediatR;
using T.Common.Domain.Common;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.ErrorCodeEnum;

namespace RedDragon.RBAC.API.Application.Common.User
{
    /// <summary>
    /// 用户注册
    /// </summary>
    public class AddUserHandler : IRequestHandler<AddUsersCommand, Result>
    {

        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 用户仓储
        /// </summary>
        public readonly IBaseRepository<Users> usersRepository;

        public AddUserHandler(IMapper mapper, IBaseRepository<Users> usersRepository)
        {
            this.mapper = mapper;
            this.usersRepository = usersRepository;
        }
        /// <summary>
        /// 用户添加注册
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(AddUsersCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            Users Users = mapper.Map<Users>(request);
            var res = await usersRepository.Add(Users);
            if (res > 0)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = res;
                result.Msg = "用户添加成功";
            }
            return result;
        }
    }
}
